﻿using System;
using System.Windows;
using System.Windows.Controls;
using Infragistics.Windows.Controls;
using Infragistics.Windows.DataPresenter;
using Infragistics.Windows.DataPresenter.Events;
using TVersiPlay.Common.Interfaces;
using TVersiPlay.MediaBrowse.ViewModels;

namespace TVersiPlay.MediaBrowse.Views
{
    /// <summary>
    /// Interaction logic for ThreeOrMoreMusicView.xaml
    /// </summary>
    public partial class ThreeOrMoreMusicView : UserControl, IView
    {
        private bool _selectionInitialized = false;

        public ThreeOrMoreMusicView(MediaBrowseViewModel vm)
        {
            InitializeComponent();
            this.DataContext = vm;
        }

        public bool PersistView
        {
            get { return false; }
        }

        private void OnAlbumSelected(object sender, Infragistics.Windows.DataPresenter.Events.SelectedItemsChangingEventArgs e)
        {
            if (this.albumCarousel.SelectedItems.Records.Count > 0 || this._selectionInitialized == false)
            {
                int newIndex = e.NewSelection.Records[0].Index;

                //DataRecord dr = this.albumCarousel.SelectedItems.Records[0] as DataRecord;
                if (newIndex >= 0)
                {
                    // Scroll the record to the center position.
                    this.ScrollCarouselItemToCenterPosition(this.albumCarousel, newIndex);
                }
            }
        }

        private void OnSelectPreviousAlbum(object sender, EventArgs e)
        {
            if (this._selectionInitialized)
            {
                if (this.albumCarousel.SelectedItems.Records.Count > 0)
                {
                    int totalRecords = this.albumCarousel.Records.Count;
                    int currentSelectedIndex = ((DataRecord)this.albumCarousel.SelectedItems.Records[0]).Index;
                    int newSelectedIndex = currentSelectedIndex - 1;
                    if (newSelectedIndex < 0)
                        newSelectedIndex = newSelectedIndex + totalRecords;

                    this.albumCarousel.ActiveRecord = this.albumCarousel.Records[newSelectedIndex];
                    this.albumCarousel.Records[newSelectedIndex].IsSelected = true;
                }
            }
            else
            {
                if (this.albumCarousel.Records.Count > 0)
                {
                    this.albumCarousel.ActiveRecord = this.albumCarousel.Records[Math.Min(this.GetCenterPositionOffset(this.albumCarousel) - 1, this.albumCarousel.Records.Count - 1)];
                    this.albumCarousel.Records[Math.Min(this.GetCenterPositionOffset(this.albumCarousel) - 1, this.albumCarousel.Records.Count - 1)].IsSelected = true;
                    this._selectionInitialized = true;
                }

            }
        }

        private void OnSelectNextAlbum(object sender, EventArgs e)
        {
            if (this._selectionInitialized)
            {
                if (this.albumCarousel.SelectedItems.Records.Count > 0)
                {
                    int totalRecords = this.albumCarousel.Records.Count;
                    int currentSelectedIndex = ((DataRecord)this.albumCarousel.SelectedItems.Records[0]).Index;
                    int newSelectedIndex = currentSelectedIndex + 1;
                    if (newSelectedIndex >= totalRecords)
                        newSelectedIndex = newSelectedIndex - totalRecords;

                    this.albumCarousel.ActiveRecord = this.albumCarousel.Records[newSelectedIndex];
                    this.albumCarousel.Records[newSelectedIndex].IsSelected = true;

                }
            }
            else
            {
                if (this.albumCarousel.Records.Count > 0)
                {
                    this.albumCarousel.ActiveRecord = this.albumCarousel.Records[Math.Min(this.GetCenterPositionOffset(this.albumCarousel) - 1, this.albumCarousel.Records.Count - 1)];
                    this.albumCarousel.Records[Math.Min(this.GetCenterPositionOffset(this.albumCarousel) - 1, this.albumCarousel.Records.Count - 1)].IsSelected = true;
                    this._selectionInitialized = true;
                }

            }
        }

        private void OnRecordsInViewChanged(object sender, RecordsInViewChangedEventArgs e)
        {
            if (this._selectionInitialized == false)
            {
                int selectedRecord = Math.Min(this.GetCenterPositionOffset(this.albumCarousel) - 1, this.albumCarousel.Records.Count - 1);
                this.albumCarousel.Records[selectedRecord].IsSelected = true;
                this.albumCarousel.ActiveRecord = this.albumCarousel.Records[selectedRecord];
                this._selectionInitialized = true;
            }
        }

        private bool ScrollCarouselItemToCenterPosition(DataPresenterBase dataPresenter, int newIndex)
        {
            int centerPositionOffset = this.GetCenterPositionOffset(dataPresenter);
            dataPresenter.ScrollInfo.SetVerticalOffset(newIndex - centerPositionOffset + 1);

            return true;
        }

        private int GetCenterPositionOffset(DataPresenterBase dataPresenter)
        {
            CarouselViewSettings viewSettings = null;

            if (dataPresenter is XamDataCarousel)
                viewSettings = ((XamDataCarousel)dataPresenter).ViewSettings;
            else if (dataPresenter is XamDataPresenter && ((XamDataPresenter)dataPresenter).View is CarouselView)
                viewSettings = ((CarouselView)((XamDataPresenter)dataPresenter).View).ViewSettings;
            else
                return 0;

            int rem;
            Math.DivRem(viewSettings.ItemsPerPage, 2, out rem);

            int centerPositionOffset = viewSettings.ItemsPerPage / 2;
            if (rem > 0)
                centerPositionOffset++;

            return centerPositionOffset;
        }

        private void OnSourceUpdated(object sender, DependencyPropertyChangedEventArgs e)
        {

        }
    }
}
